package com.riteny.paymentgateway.swifypass.sdk;

import com.riteny.paymentgateway.swifypass.exception.SwiftPassPaymentGatewayException;
import com.riteny.paymentgateway.swifypass.config.SwiftPassPaymentGatewaySetting;
import com.riteny.paymentgateway.swifypass.entity.SwiftPassOrderQueryParams;
import com.riteny.paymentgateway.swifypass.entity.SwiftPassOrderSubmitParams;
import com.riteny.paymentgateway.swifypass.entity.SwiftPassQueryPaymentOrderResponseEntity;
import com.riteny.paymentgateway.swifypass.entity.SwiftPassSubmitPaymentOrderResponseEntity;
import com.riteny.paymentgateway.swifypass.service.SwiftPassAliPayPaymentGatewayService;
import com.riteny.paymentgateway.swifypass.service.SwiftPassBocPaymentGatewayService;

/**
 * @author Riteny
 * 2022/1/6  17:10
 */
public abstract class AbstractSwiftPassPaymentGatewaySdkService {

    public static final int PAYMENT_GATEWAY_ALI_PAY = 1;
    public static final int PAYMENT_GATEWAY_WECHAT = 2;
    public static final int PAYMENT_GATEWAY_BOC = 3;

    private SwiftPassAliPayPaymentGatewayService aliPayPaymentGatewayService;

    private SwiftPassBocPaymentGatewayService bocPaymentGatewayService;

    AbstractSwiftPassPaymentGatewaySdkService() {
        this.bocPaymentGatewayService = new SwiftPassBocPaymentGatewayService();
        this.aliPayPaymentGatewayService = new SwiftPassAliPayPaymentGatewayService();
    }

    SwiftPassSubmitPaymentOrderResponseEntity submitUnityOrder(Integer paymentGatewayType, SwiftPassPaymentGatewaySetting setting
            , String orderUuid, String deviceId, Integer totalFee, String body, String nonceStr) {

        switch (paymentGatewayType) {
            case PAYMENT_GATEWAY_ALI_PAY: {
                return submitAlipayUnityOrder(setting, orderUuid, deviceId, totalFee, body, nonceStr);
            }
            case PAYMENT_GATEWAY_BOC: {
                return submitBocUnityOrder(setting, orderUuid, deviceId, totalFee, body, nonceStr);
            }
            default: {
                throw new SwiftPassPaymentGatewayException(SwiftPassPaymentGatewayException.UNKNOWN_ERROR, "Unknown payment gateway");
            }
        }
    }

    private SwiftPassSubmitPaymentOrderResponseEntity submitAlipayUnityOrder(SwiftPassPaymentGatewaySetting setting
            , String orderUuid, String deviceId, Integer totalFee, String body, String nonceStr) {

        SwiftPassOrderSubmitParams params = new SwiftPassOrderSubmitParams(setting.getPaymentGatewayUrl()
                , setting.getPaymentGatewayMchId(), setting.getPaymentGatewayKey(), orderUuid, deviceId, totalFee
                , setting.getOrderExpireTime(), body, nonceStr, setting.getCallBackUrl());

        return aliPayPaymentGatewayService.submitUnityOrder(params);
    }

    private SwiftPassSubmitPaymentOrderResponseEntity submitBocUnityOrder(SwiftPassPaymentGatewaySetting setting
            , String orderUuid, String deviceId, Integer totalFee, String body, String nonceStr) {

        SwiftPassOrderSubmitParams params = new SwiftPassOrderSubmitParams(setting.getPaymentGatewayUrl()
                , setting.getPaymentGatewayMchId(), setting.getPaymentGatewayKey(), orderUuid, deviceId, totalFee
                , setting.getOrderExpireTime(), body, nonceStr, setting.getCallBackUrl());

        return bocPaymentGatewayService.submitUnityOrder(params);
    }


    SwiftPassQueryPaymentOrderResponseEntity queryUnityOrder(Integer paymentGatewayType
            , SwiftPassPaymentGatewaySetting setting, String orderNo) {

        switch (paymentGatewayType) {
            case PAYMENT_GATEWAY_ALI_PAY: {
                return queryAlipayUnityOrder(setting, orderNo);
            }
            case PAYMENT_GATEWAY_BOC: {
                return queryBocUnityOrder(setting, orderNo);
            }
            default: {
                throw new SwiftPassPaymentGatewayException(SwiftPassPaymentGatewayException.UNKNOWN_ERROR, "Unknown payment gateway");
            }
        }
    }

    private SwiftPassQueryPaymentOrderResponseEntity queryAlipayUnityOrder(SwiftPassPaymentGatewaySetting setting, String orderNo) {

        SwiftPassOrderQueryParams params = new SwiftPassOrderQueryParams(setting.getPaymentGatewayUrl()
                , setting.getPaymentGatewayMchId(), setting.getPaymentGatewayKey(), orderNo);

        return aliPayPaymentGatewayService.queryUnityOrder(params);
    }

    private SwiftPassQueryPaymentOrderResponseEntity queryBocUnityOrder(SwiftPassPaymentGatewaySetting setting, String orderNo) {

        SwiftPassOrderQueryParams params = new SwiftPassOrderQueryParams(setting.getPaymentGatewayUrl()
                , setting.getPaymentGatewayMchId(), setting.getPaymentGatewayKey(), orderNo);

        return bocPaymentGatewayService.queryUnityOrder(params);
    }

}
